<?php
    $xtpl_changeprofile     = new XTemplate("view/member/changeprofile.htm");
    
    $id = $_GET['id'];
    
    $sql    = "SELECT * FROM member WHERE id_m='" . $id . "'";
    $result = mysql_query($sql);
    $row    = mysql_fetch_array($result);
    
    $sql_date   = "SELECT DAY(birthday), MONTH(birthday), YEAR(birthday) FROM member WHERE id_m='" . $id . "'";
    $re_date    = mysql_query($sql_date);
    $row_date   = mysql_fetch_row($re_date);
    
    $day    =   '<select name="day">';
    $day    .=  '<option value="0">Ngày</option>';
    for($i=1; $i<=31; $i++){
        $day    .= '<option value="' . $i . '">' . $i . '</option>';
        if($i == $row_date[0]){
            $day    .= '<option selected="' . $row_date[0] . '">' . $row_date[0] . '</option>';
        }
    }
    $day    .= '</select>';
    
    $month  =   '<select name="month">';
    $month  .=  '<option value="0">Tháng</option>';
    for($i=1; $i<=12 ; $i++){
        $month  .= '<option value="' . $i . '">' . $i . '</option>';
        if($i == $row_date[1]){
            $month  .= '<option selected="' . $row_date[1] . '">' . $row_date[1] . '</option>';
        }
    }
    $month  .= '</select>';
    
    $year   =   '<select name="year">';
    $year   .=  '<option value="0">Năm</option>';
    for($i=1950; $i<=2011 ; $i++){
        $year   .= '<option value="' . $i . '">' . $i . '</option>';
        if($i == $row_date[2]){
            $year   .= '<option selected="' . $row_date[2] . '">' . $row_date[2] . '</option>';
        }
    }
    $year   .= '</select>';
    
    $gender = "";
    if($row['gender'] == 'Nam'){
        $gender .= '<input type="radio" name="gender" checked="checked" value="Nam" />Nam';
        $gender .= '<input type="radio" name="gender" value="Nữ" />Nữ';
    }
    else if($row['gender'] == 'Nữ'){
        $gender .= '<input type="radio" name="gender" value="Nam" />Nam';
        $gender .= '<input type="radio" name="gender" checked="checked" value="Nữ" />Nữ';        
    }
    else{
        $gender .= '<input type="radio" name="gender" value="Nam" />Nam';
        $gender .= '<input type="radio" name="gender" value="Nữ" />Nữ';         
    }
    
    $xtpl_changeprofile     -> assign('fullname', $row['fullname']);
    $xtpl_changeprofile     -> assign('day', $day);
    $xtpl_changeprofile     -> assign('month', $month);
    $xtpl_changeprofile     -> assign('year', $year);
    $xtpl_changeprofile     -> assign('gender', $gender);
    $xtpl_changeprofile     -> assign('address', $row['address']);
    $xtpl_changeprofile     -> assign('email', $row['email']);
    $xtpl_changeprofile     -> assign('phone', $row['phone']);
    
    if(isset($_POST['submit'])){
        $fullName       = $_POST['fullname'];
        $day            = $_POST['day'];
        $day            = ($day == 0) ? ($day = '00') : $day;
        $month          = $_POST['month'];
        $month          = ($month == 0) ? ($month = '00') : $month;
        $year           = $_POST['year'];
        $year           = ($year == 0) ? ($year = '0000') : $year;
        $birthday       = $year . '-' . $month . '-' . $day;
        $gender         = $_POST['gender'];
        $gender         = ($gender == "") ? ($gender = '') : $gender;
        $address        = $_POST['address'];
        $email          = $_POST['email'];
        $phone          = $_POST['phone'];
        
        $sql    = "UPDATE member SET fullname   = '" . $fullName . "',
                                     birthday   = '" . $birthday . "',
                                     gender     = '" . $gender . "',
                                     address    = '" . $address . "',
                                     email      = '" . $email . "',
                                     phone      = '" . $phone . "'
                   WHERE id_m = '" . $id . "'";
        $result = mysql_query($sql);
        if($result){
            header("location:index.php?page=homepage&module=successchange&id=" . $id . "");
        }
    }
    
    $xtpl_changeprofile     -> parse("CHANGEPROFILE");
    $mid                    = $xtpl_changeprofile -> text("CHANGEPROFILE");
?>